<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form :form='form' slot='detail'>
        <a-row>
          <a-col :span='12'>
            <a-form-item label='项目简称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                v-decorator="['projectName',validatorRules.projectShortName]"
                :trigger-change='true'
                org-fields='id,short_name'
                dest-fields='projectId,projectName'
                code='pm_project_select'
                @callback='popupCallback'
                :disabled='isOk'
              />
              <input v-decorator="['projectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='专题名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                v-decorator="['subjectName',validatorRules.subjectName]"
                :trigger-change='true'
                org-fields='id,subject_name'
                dest-fields='subjectId,subjectName'
                code='pm_subject_select'
                :param='{projectId: model.projectId}'
                @callback='popupCallback'
                :disabled='isOk'
              />
              <input v-decorator="['subjectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='风险类型' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['riskType',validatorRules.riskType]" placeholder='请选择风险类型'
                                 :trigger-change='true' dictCode='risk_type' :disabled='isOk'/>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='风险等级' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['riskGrade',validatorRules.riskGrade]" placeholder='请选择风险等级'
                                 :trigger-change='true' dictCode='risk_grade' :disabled='isOk' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='状态' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['riskState',validatorRules.riskState]" placeholder='请选择风险状态'
                                 :trigger-change='true' dictCode='icm_pm_projectRisks' :disabled='isOk'/>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='负责人' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-select-user-by-dep :multi='false' v-decorator="['dutyPersion', validatorRules.charger]" :disabled='isOk'
              />
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='风险描述' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['riskDes',validatorRules.riskDes]" :disabled='isOk'></a-textarea>
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='影响分析' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['affect']" :autoSize='{minRows:4}' placeholder='请输入影响分析' :disabled='isOk' />
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='应对措施' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['solutions']" :autoSize='{minRows:4}' placeholder='请输入应对措施' :disabled='isOk'></a-textarea>
            </a-form-item>
          </a-col>
          <a-col :span="12"  v-if='isOk'>
            <a-form-item label="确认人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep v-decorator="['confirmorName',validatorRules.confirmorName]" />
            </a-form-item>
          </a-col>
          <a-col :span='24' v-if='isOk'>
            <a-form-item label='确认意见' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['confirmorDes',validatorRules.confirmorDes]" :autoSize='{minRows:4}' placeholder='请输入确认意见'></a-textarea>
            </a-form-item>
          </a-col>

          <a-col v-if='showFlowSubmitButton' :span='24' style='text-align: center'>
            <a-button @click='submitForm'>提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <hf-project-select-modal ref='hfProjectSelectModal' :multiple='false' @hf-pm-project-select='hfPmProjectSelect' />
    <hf-subject-select-modal ref='hfSubjectSelectModal' :multiple='false' @hf-subject-select='hfSubjectSelect' />
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import HfProjectSelectModal from '@views/hifar/pm/modules/HfProjectSelectModal'
import HfSubjectSelectModal from '@views/hifar/pm/modules/HfSubjectSelectModal'


export default {
  name: 'HfPmProjectRisksForm',
  components: {
    HfProjectSelectModal,
    HfSubjectSelectModal
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      confirmLoading: false,
      validatorRules: {
        confirmorDes: {
          rules: [
            { required: true, message: '请填写确认意见!' }
          ]
        },
        confirmorName: {
          rules: [
            { required: true, message: '请选择确认人!' }
          ]
        },
        charger: {
          rules: [
            { required: true, message: '请选择负责人!' }
          ]
        },
        projectShortName: {
          rules: [
            { required: true, message: '请选择项目简称!' }
          ]
        },
        riskType: {
          rules: [
            { required: true, message: '请选择风险类型!' }
          ]
        },
        riskGrade: {
          rules: [
            { required: true, message: '请选择风险等级!' }
          ]
        },
        riskDes: {
          rules: [
            { required: true, message: '请输入风险描述!' }
          ]
        },
        subjectName: {
          rules: [
            { required: true, message: '请选择专题!' }
          ]
        },
        riskState: {
          rules: [
            { required: true, message: '请选择状态!' }
          ],
          initialValue: '0'
        }
      },
      url: {
        add: '/pm/hfPmProjectRisks/add',
        edit: '/pm/hfPmProjectRisks/edit',
        confim: '/pm/hfPmProjectRisks/confim',
        queryById: '/pm/hfPmProjectRisks/queryById'
      },
      isOk: false,
      fields: [
        'createBy', 'createTime', 'deleteFlag', 'remarks', 'riskCode', 'projectId', 'projectCode',
        'projectName', 'riskType', 'riskGrade', 'riskDes', 'affect', 'solutions', 'dutyPersion', 'confirmorId', 'confirmorDes',
        'confirmorName', 'subjectId', 'subjectName', 'subjectCode'
      ],
      defaultModel: {}
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    add() {
      let defaultObj = {}
      let {
        selectedSubName,
        selectedName,
        selectedKeys,
        selectedSubKeys,
        selectedCode,
        selectedSubCode,
        selectedTreeType
      } = this.defaultModel
      if (selectedTreeType === 'project') {
        defaultObj = { projectName: selectedName, projectId: selectedKeys, projectCode: selectedCode }
      } else if (selectedTreeType === 'subject') {
        defaultObj = {
          projectName: selectedName,
          projectId: selectedKeys,
          projectCode: selectedCode,
          subjectName: selectedSubName,
          subjectCode: selectedSubCode,
          subjectId: selectedSubKeys
        }
      }
      this.edit(defaultObj)
    },
    edit(record) {
      console.log(this.isOk)
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    confim(record) {
      console.log(this.isOk)
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            if (this.isOk) {
              httpurl += this.url.confim
              method = 'put'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
    projectShortNameClick() {
      this.$nextTick(() => {
        this.$refs.hfProjectSelectModal.title = '选择项目'
        this.$refs.hfProjectSelectModal.visible = true
      })
    },
    hfPmProjectSelect(data) {
      this.form.setFieldsValue({
        projectId: data.id,
        projectName: data.shortName,
        projectCode: data.code
      })
    },
    subjectNameClick() {
      this.$nextTick(() => {
        this.$refs.hfSubjectSelectModal.title = '选择主题'
        this.$refs.hfSubjectSelectModal.visible = true
      })
    },
    hfSubjectSelect(data) {
      this.form.setFieldsValue({
        subjectId: data.id,
        subjectName: data.subjectName,
        subjectCode: data.subjectCode
      })
    }
  }
}
</script>